package com.bzzoneit.newsys.service.admin.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bzzoneit.newsys.dao.admin.UserMapper;
import com.bzzoneit.newsys.entity.admin.User;
import com.bzzoneit.newsys.service.admin.UserService;
import com.bzzoneit.newsys.utils.exception.ServiceException;
import com.bzzoneit.newsys.utils.page.Page;
import com.google.common.collect.Maps;

@Service
public class UserServiceImpl implements UserService {
	
	protected static Logger logger =  LoggerFactory.getLogger("service");
	
	@Autowired
	private UserMapper userMapper;

	@Transactional
	public User login(User user) {
		return userMapper.getUser(user);
	}

	public Page<User> getUserList(Page<User> page) {
		logger.info("servcie start");
		int pg = page.getPageNo();
		int pz = page.getNumPerPage();
		User user=page.getT();
		int offset = 0;
		int limit = 0;
		if (pg > 0)
			offset = (pg - 1) * pz;
		else
			offset = 0;
		limit = pz;
		String orderString="";		
		if(StringUtils.isNotEmpty(page.getOrderBy())){
			orderString=page.getOrderBy();
			if(StringUtils.isNotEmpty(page.getOrder())){
				orderString+=" "+page.getOrder();
			}
			else{
				orderString+=" asc";
			}
			
		}
		else{
			orderString=" create_date desc"; //修改时间倒序排列
		}	
		user.setOrderString(orderString);
		List<User> result = userMapper.find(new RowBounds(offset, limit),user);
		page.setResult(result);
		page.setTotalCount(userMapper.getTotal(user));
		return page;
	}

	public void insertUser(User user) {
		user.setCreateDate(new Date());
		userMapper.insertUser(user);		
	}

	public void updateUser(User user) {
		user.setCreateDate(new Date());
		userMapper.updateUser(user);
		
	}

	/**
	 * 删除用户的同时需要把用户对应的角色关系在t_user_role删除
	 */
	@Transactional
	public void deleteUser(Long id) {
		if (isSupervisor(id)) {
			logger.info("操作员{}尝试删除超级管理员用户", SecurityUtils.getSubject().getPrincipal());
			throw new ServiceException("不能删除超级管理员用户");
		}
		 userMapper.deleteOne(id);
		 userMapper.deleteUserRole(id);	 
	}
	
	/**
	 * 判断是否超级管理员.
	 */
	private boolean isSupervisor(Long id) {
		return id == 1;
	}

	public List<User> getUserListByRoleid(Long id) {
		return userMapper.getUserListByRoleid(id);
	}

	public List<User> getAllUserList() {
		// TODO Auto-generated method stub
		return userMapper.getAllUserList();
	}



	@Override
	public User login(String username, String password) {
		Map<String, Object> param =Maps.newHashMap();
		param.put("name", username);
		param.put("password", password);
		return userMapper.login(param);
	}

	@Override
	public User selectOne(long id) {
		// TODO Auto-generated method stub
		return userMapper.selectOne(id);
	}




}
